import Vue from 'vue'
import App from './App.vue'

import FastClick from 'fastclick'

import router from './router/index'
import store from './store/index'
import {getStore} from '@/config/global'
import {loginWxAuthorize} from '@/utils/utils'

// import vConsole from 'vconsole'
//
// let vconsole = null;

Vue.config.productionTip = false

if ('addEventListener' in document) {

    document.addEventListener('DOMContentLoaded', function() {
        FastClick.attach(document.body);
    }, false);

    // document.addEventListener('touchstart', function(events) {
    //
    //     if (events.touches.length >= 5) {
    //
    //         if (vconsole == null){
    //             vconsole = new vConsole()
    //         } else {
    //             var m = document.getElementById("__vconsole");
    //             m.parentNode.removeChild(m);
    //             vconsole = null;
    //         }
    //
    //     }
    // }, false);

}

FastClick.prototype.focus = function(targetElement) {

    var length;

    // FastClick 点击不灵敏做的兼容

    //兼容处理:在iOS7中，有一些元素（如date、datetime、month等）在setSelectionRange会出现TypeError

    //这是因为这些元素并没有selectionStart和selectionEnd的整型数字属性，所以一旦引用就会报错，因此排除这些属性才使用setSelectionRange方法

    if (deviceIsIOS && targetElement.setSelectionRange && targetElement.type.indexOf('date') !== 0 && targetElement.type !== 'time' && targetElement.type !== 'month' && targetElement.type !== 'email') {

        length = targetElement.value.length;

        targetElement.setSelectionRange(length, length);

        /*修复bug ios 11.3不弹出键盘，这里加上聚焦代码，让其强制聚焦弹出键盘*/

        targetElement.focus();

    } else {

        targetElement.focus();

    }

};


router.beforeEach((to, from, next) => {

    let userInfo = JSON.parse(getStore('userInfo'));

    if (to.matched.some(function (item) {
        return item.meta.requireAuth
    }) && !userInfo) {

        if (process.env.NODE_ENV === "development"){
            next('/login')
        } else {
            loginWxAuthorize();
        }

    } else{
        if (to.matched.some(function (item) {
            return item.meta.userOnly
        }) && (!userInfo || userInfo.role !== 'USER')) {
            next('/dashboard')
        }else {
            next()
        }

    }

})


import '@/style/common.less'
import '@/plugins/vant'
import '@/config/rem.js'
import '@/config/filters'

// import '@/plugins/vconsole'

new Vue({
    router,
    store,
  render: h => h(App)
}).$mount('#app')
